package com.biostime.springdraw.modules.sd.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.springdraw.common.config.Global;
import com.biostime.springdraw.common.persistence.Page;
import com.biostime.springdraw.common.utils.StringUtils;
import com.biostime.springdraw.common.web.BaseController;
import com.biostime.springdraw.modules.sd.entity.Share;
import com.biostime.springdraw.modules.sd.service.GridService;
import com.biostime.springdraw.modules.sd.service.ShareService;
import com.biostime.springdraw.modules.sd.service.ShareawardService;
import com.biostime.springdraw.modules.sd.service.SharegridService;
import com.biostime.springdraw.modules.sd.entity.Grid;

/**
 * 分享Controller
 * @author chenweicheng
 * @version 2016-05-03
 */
@Controller
@RequestMapping(value = "${adminPath}/sd/share")
public class ShareController extends BaseController {

	@Autowired
	private ShareService ShareService;
	
	@Autowired
	private GridService gridService;
	
	@Autowired
	private SharegridService sharegridService;
	
	@Autowired
	private ShareawardService shareawardService;
	
	@ModelAttribute
	public Share get(@RequestParam(required=false) String id) {
		Share entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = ShareService.get(id);
		}
		if (entity == null){
			entity = new Share();
		}
		return entity;
	}
	
	@RequiresPermissions("sd:share:view")
	@RequestMapping(value = {"list", ""})
	public String list(Share Share, HttpServletRequest request, HttpServletResponse response, Model model) {
		//回收过期的宫格还有奖品
		sharegridService.recoveryGrid(shareawardService.recoveryAward());
		Page<Share> page = ShareService.findPage(new Page<Share>(request, response), new Share()); 
		List<Share> shareList = ShareService.findShare(new Share());
		if(shareList != null && !shareList.isEmpty()){
			for(Share share : shareList){
				share.setGridList(sharegridService.getByShareId(share.getId()));
			}
		}
		page.setList(shareList);
		List<Grid> gridList = gridService.findList(new Grid());
		model.addAttribute("page", page);
		model.addAttribute("gridList", gridList);
		return "modules/sd/ShareList";
	}

	@RequiresPermissions("sd:share:view")
	@RequestMapping(value = "form")
	public String form(Share Share, Model model) {
		model.addAttribute("Share", Share);
		return "modules/sd/ShareForm";
	}

	@RequiresPermissions("sd:share:edit")
	@RequestMapping(value = "save")
	public String save(Share Share, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, Share)){
			return form(Share, model);
		}
		ShareService.save(Share);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/sd/share/?repage";
	}
	
	@RequiresPermissions("sd:share:edit")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, Share Share, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				ShareService.delete(new Share(id));
			}
		}else{
			ShareService.delete(Share);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/sd/share/?repage";
	}

}